<?php

/**
 * Manage "order" table 
 * 
 * @property int $order_product_id
 * @property int $order_id is foreign key of orders table
 * @property int $model_id to determine what model is used 
 * @property int $item_id what product is chosen
 * @property int $user_id
 * @property int $total_order
 * @property float $total_purchased
 * @property int $real_width the original width of image
 * @property int $custom_width the custom width of image
 * @property int $degree
 * @property float $rate rate of original image and custom image
 * @property string $position 
 * @property string $image_uploaded 
 * @property string $image_designed 
 */
class OrderDetail extends CActiveRecord {

    const MODEL_PRODUCT = 0;
    const MODEL_CUSTOM_PHONE = 1;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{custom_phone}}';
    }

    public function rules() {
	return array(
	    array('order_product_id, order_id, item_id, user_id, total_order, total_purchased, real_width, custom_width, degree, rate, position, image_uploaded, image_designed ', 'safe')
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'order' => array(self::BELONGS_TO, 'Order', 'order_id'),
	    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
	    'customphone' => array(self::BELONGS_TO, 'CustomPhone', 'item_id'),
	    'product' => array(self::BELONGS_TO, 'Product', 'itemt_id'),
	);
    }

    /**
     * 
     * 
     * @param int $type is comment or post
     * @return \CActiveDataProvider
     */
    public function search($type) {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	if ($type == self::POST_TYPE) {
	    $criteria->with = array("user", "posts");
	    $criteria->compare('posts.title', $this->postTitle, true);
	} else {
	    $criteria->with = array("user", "comment");
	    $criteria->compare('comment.content', $this->commentText, true);
	}
	$criteria->addCondition('t.is_ignore!=' . self::IGNORE);
	$criteria->addCondition('t.type=' . $type);
	$criteria->compare('user.username', $this->sender, true);

	$criteria->compare('message', $this->message, true);


	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    't.time_created' => CSort::SORT_DESC
		)
	    )
	));
    }

}

